package com.estun.backroundManagementService.config.factoryMes;

import com.alibaba.fastjson.JSON;
import com.estun.backroundManagementService.config.properties.ApiInfoProperties;
import com.estun.backroundManagementService.config.properties.LineInfoProperties;
import com.estun.backroundManagementService.config.properties.MesInfoProperties;
import com.estun.backroundManagementService.config.properties.StationApiInfoProperties;
import com.estun.backroundManagementService.domain.factoryMes.request.base.BaseMesRequest;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import com.estun.backroundManagementService.config.CustomApiException;

import java.time.LocalDateTime;

@Slf4j
@Component
@AllArgsConstructor
public class ApiCaller {

    private final RestTemplate restTemplate;

    private final MesInfoProperties mesInfoProperties;

    private final StationApiInfoProperties stationApiInfoProperties;

    private final LineInfoProperties lineInfoProperties;


    public ResponseEntity<String> callApi(String url, Object requestBody, HttpMethod method) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("appId", mesInfoProperties.getAppId());
        headers.add("appKey", mesInfoProperties.getAppKey());
        HttpEntity<Object> requestEntity = new HttpEntity<>(requestBody, headers);
        try {
            return restTemplate.exchange(mesInfoProperties.getIpPort() + url, method, requestEntity, String.class);
        } catch (Exception e) {
            log.error("【工厂mes接口调用异常】url:{} , 异常信息:{}", url, e.getMessage());
            throw new CustomApiException("工厂MES接口调用失败: " + url, e);
        }
    }


    public ResponseEntity<String> callStationApi(String url, Object requestBody, HttpMethod method) {
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<Object> requestEntity = new HttpEntity<>(requestBody, headers);
        return restTemplate.exchange(stationApiInfoProperties.getIpPort() + url, method, requestEntity, String.class);
    }

    public <R extends BaseMesRequest, T> T post(ApiInfoProperties.ApiInfo apiInfo, R requestBody, Class<T> responseType) {
        requestBody.setFactoryCode(lineInfoProperties.getFactoryCode());
        if (!"Base002_Material".equals(apiInfo.getServiceId())) {
            requestBody.setLineCode(lineInfoProperties.getLineCode());
            requestBody.setUpdateTime(LocalDateTime.now());
        }
        requestBody.setRequestTime(LocalDateTime.now());
        requestBody.setServiceId(apiInfo.getServiceId());
        log.info("【工厂接口调用】api serviceId:{} 入参:{}", apiInfo.getServiceId(), requestBody);
        long beginTime = System.currentTimeMillis();
        ResponseEntity<String> responseEntity = callApi(apiInfo.getUrl(), requestBody, HttpMethod.POST);
        log.info("【工厂接口调用】api serviceId:{} 返回:{} 耗时:{} ",
                apiInfo.getServiceId(), responseEntity, System.currentTimeMillis() - beginTime);
        String responseBody = responseEntity.getBody();
        return JSON.parseObject(responseBody, responseType);
    }

    public <T> T get(String url, Object requestBody, Class<T> responseType) {
        ResponseEntity<String> responseEntity = callApi(url, requestBody, HttpMethod.GET);
        String responseBody = responseEntity.getBody();
        return JSON.parseObject(responseBody, responseType);
    }
}
